/*-----------------------------------------------File Info------------------------------------------------
** File Name:               bspExti.c  
** Last modified date:      20220724
** Last version:            V0.1
** Description:             外部中断
*/
#include "bspExti.h"


static extiCallback gExtiCb[16];



void extiConfig(tsCfgExti *cfg, extiCallback cb)
{
	gExtiCb[cfg->exti_pin] = cb;
    /* enable clock */
    rcu_periph_clock_enable(cfg->gpioRcu);
    gpio_mode_set(cfg->gpioPeriph, GPIO_MODE_INPUT, cfg->pull, cfg->pin);

    /* enable the CFGCMP clock */
    rcu_periph_clock_enable(RCU_CFGCMP);

#if BSP_MCU_TYPE == BSP_MCU_GD32E23x	
    /* enable and set key EXTI interrupt to the specified priority */
    nvic_irq_enable(cfg->nvicIrq, cfg->nvicIrqPriority);
#elif BSP_MCU_TYPE == BSP_MCU_GD32F3x0	
    /* enable and set key temper EXTI interrupt to the lower priority */
    nvic_irq_enable(cfg->nvicIrq,  cfg->nvicIrqPriority,  cfg->nvicIrqSubPriority);	
#endif
    /* connect key EXTI line to key GPIO pin */
    syscfg_exti_line_config(cfg->exti_port, cfg->exti_pin);

    /* configure key EXTI line */
    exti_init(cfg->linex, EXTI_INTERRUPT, cfg->trig_type);
	
	exti_interrupt_enable(cfg->linex);
	
    exti_interrupt_flag_clear(cfg->linex);
}


void extiDisableInt(tsCfgExti *cfg)
{	
	exti_interrupt_flag_clear(cfg->linex);
	exti_interrupt_disable(cfg->linex);			
}


void extiEnsableInt(tsCfgExti *cfg)
{	
	exti_interrupt_flag_clear(cfg->linex);
	exti_interrupt_enable(cfg->linex);		
}

void EXTI0_1_IRQHandler(void)
{
    if(RESET != exti_interrupt_flag_get(EXTI_0)) 
	{
        exti_interrupt_flag_clear(EXTI_0);
		if(gExtiCb[0] != NULL)
			gExtiCb[0]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_1)) 
	{
        exti_interrupt_flag_clear(EXTI_1);
		if(gExtiCb[1] != NULL)
			gExtiCb[1]();
    }	
}

void EXTI2_3_IRQHandler(void)
{
    if(RESET != exti_interrupt_flag_get(EXTI_2)) 
	{
        exti_interrupt_flag_clear(EXTI_2);
		if(gExtiCb[2] != NULL)
			gExtiCb[2]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_3)) 
	{
        exti_interrupt_flag_clear(EXTI_3);
		if(gExtiCb[3] != NULL)
			gExtiCb[2]();
    }	
}

void EXTI4_15_IRQHandler(void)
{
    if(RESET != exti_interrupt_flag_get(EXTI_4)) 
	{
        exti_interrupt_flag_clear(EXTI_4);
		if(gExtiCb[4] != NULL)
			gExtiCb[4]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_5)) 
	{
        exti_interrupt_flag_clear(EXTI_5);
		if(gExtiCb[5] != NULL)
			gExtiCb[5]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_6)) 
	{
        exti_interrupt_flag_clear(EXTI_6);
		if(gExtiCb[6] != NULL)
			gExtiCb[6]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_7)) 
	{
        exti_interrupt_flag_clear(EXTI_7);
		if(gExtiCb[7] != NULL)
			gExtiCb[7]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_8)) 
	{
        exti_interrupt_flag_clear(EXTI_8);
		if(gExtiCb[8] != NULL)
			gExtiCb[8]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_9)) 
	{
        exti_interrupt_flag_clear(EXTI_9);
		if(gExtiCb[9] != NULL)
			gExtiCb[9]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_10)) 
	{
        exti_interrupt_flag_clear(EXTI_10);
		if(gExtiCb[10] != NULL)
			gExtiCb[10]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_11)) 
	{
        exti_interrupt_flag_clear(EXTI_11);
		if(gExtiCb[11] != NULL)
			gExtiCb[11]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_12)) 
	{
        exti_interrupt_flag_clear(EXTI_12);
		if(gExtiCb[12] != NULL)
			gExtiCb[12]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_13)) 
	{
        exti_interrupt_flag_clear(EXTI_13);
		if(gExtiCb[13] != NULL)
			gExtiCb[13]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_14)) 
	{
        exti_interrupt_flag_clear(EXTI_14);
		if(gExtiCb[14] != NULL)
			gExtiCb[14]();
    }
    if(RESET != exti_interrupt_flag_get(EXTI_15)) 
	{
        exti_interrupt_flag_clear(EXTI_15);
		if(gExtiCb[15] != NULL)
			gExtiCb[15]();
    }	
}
